﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarbonMeter
{
    public partial class frmAddUser : Form
    {
        private System.Drawing.Icon rowIcon;
        private static string admin = "Admin";
        private static string user = "Kullanıcı";
        private static string manager = "Yönetici";

        //TODO: bu form hem modal olmali hemde buyutunce ail program kapatilacak kadar buyuk olmamali:dynamic olarak max boyunu ana ekrana gore ayarlanmali
        //TODO: setupda icon yeri degisecek!!!
        //TODO: satır numaralandırması kaldırılmalı!!
        public frmAddUser()
        {
            InitializeComponent();
            rowIcon = new System.Drawing.Icon(@"D:\SOFTWARE_PROJECTS\Projects\CarbonMeter\CarbonMeter\images\personal.ico");
        }

        private void frmAddUser_Load(object sender, EventArgs e)
        {
            customDataGridViewUsers.DataSource = Database.getInstance().getUsers();
            btnDegistir.Enabled = false;
        }

        private void customDataGridViewUsers_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Graphics graphics = e.Graphics;

            //Set Image dimension - User's choice
            int iconHeight = 16;
            int iconWidth = 16;

            //Set x/y position - As the center of the RowHeaderCell
            int xPosition = e.RowBounds.X; // + (customDataGridViewUsers.RowHeadersWidth / 3);
            int yPosition = e.RowBounds.Y +
            ((customDataGridViewUsers.Rows[e.RowIndex].Height - iconHeight) / 2);

            Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
            graphics.DrawIcon(rowIcon, rectangle);
        }

        //TODO: userId degistirmede karilasilan sorun duzeltilmeli
        private void btnDegistir_Click(object sender, EventArgs e)
        {
            try
            {
                Database.getInstance().updateUsers(int.Parse(txtId.Text), txtPassword.Text, txtName.Text, txtSurName.Text,
                (string)cmbGrubu.SelectedItem, txtEMail.Text, (cmbDurumu.SelectedIndex == 0));
            }
            catch (Exception myEx)
            {
                MessageBox.Show(myEx.StackTrace);
            }
            customDataGridViewUsers.DataSource = Database.getInstance().getUsers();
            
            cleanForm();
            btnKaydet.Enabled = true;
            btnDegistir.Enabled = false;
        }

        //TODO: kaydette hata oldugunda 1 kez, degistirde de ayni hata tekrar ediyor her seferinde
        //bir hatadan sonra ne degistirme ne yeni kayit ekleyebiliyor
        private void btnKaydet_Click(object sender, EventArgs e)
        {
            try
            {
                Database.getInstance().addUsers(int.Parse(txtId.Text), txtPassword.Text, txtName.Text, txtSurName.Text,
                    (string)cmbGrubu.SelectedItem, txtEMail.Text, (cmbDurumu.SelectedIndex == 0));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            customDataGridViewUsers.DataSource = Database.getInstance().getUsers();
            cleanForm();
        }

        private void customDataGridViewUsers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnKaydet.Enabled = false;
            btnDegistir.Enabled = true;
            DataGridViewRow row = customDataGridViewUsers.CurrentRow;

            txtName.Text = row.Cells["name"].Value.ToString();
            txtSurName.Text = row.Cells["surname"].Value.ToString();
            txtEMail.Text = row.Cells["email"].Value.ToString();
            txtId.Text = row.Cells["userid"].Value.ToString();
            txtPassword.Text = row.Cells["password"].Value.ToString();

            cmbDurumu.SelectedIndex = (bool)row.Cells["active"].Value ? 0 : 1;

            string grubu = (string)row.Cells["role"].Value.ToString();
            if (grubu[0] == admin[0])
            {
                cmbGrubu.SelectedIndex = 0;
            }
            else if (grubu[0] == user[0])
            {
                cmbGrubu.SelectedIndex = 1;
            }
            else if (grubu[0] == manager[0])
            {
                cmbGrubu.SelectedIndex = 2;
            }
        }

        private void cleanForm()
        {
            txtEMail.Text = "";
            txtId.Text = "";
            txtName.Text = "";
            txtPassword.Text = "";
            txtSurName.Text = "";
            cmbGrubu.SelectedItem = null;
            cmbDurumu.SelectedItem = null;
        }

    }
}
